package com.coooliang.apibook;

import com.coooliang.utils.RouteUtil;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Paint;
import javafx.scene.text.TextAlignment;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;


public class HelloController {
    @FXML
    private Stage stage;

    @FXML
    private Scene scene;

    @FXML
    private BorderPane rootPane;

    @FXML
    private AnchorPane rightPane;

    @FXML
    private ImageView logoImageView;

    @FXML
    private ImageView closeButton;

    @FXML
    private ImageView createImageView;

    @FXML
    private ImageView openImageView;

    @FXML
    private void onShowing() {
        logoImageView.setImage(new Image(getClass().getResourceAsStream("/assets/white_logo.png")));
        closeButton.setImage(new Image(getClass().getResourceAsStream("/assets/close.png")));
        createImageView.setImage(new Image(getClass().getResourceAsStream("/assets/add.png")));
        openImageView.setImage(new Image(getClass().getResourceAsStream("/assets/open.png")));

        float y;
        for (int i = 0; i < 8; i++) {
            y = 10 + 50 * i;
            Pane historyCell = new Pane();
            historyCell.setLayoutX(10);
            historyCell.setLayoutY(y);
            historyCell.setPrefHeight(40);
            historyCell.setPrefWidth(280);

            ImageView icon = new ImageView();
            icon.setLayoutX(10);
            icon.setLayoutY(10);
            icon.setFitWidth(20);
            icon.setFitHeight(20);
            icon.setImage(new Image(getClass().getResourceAsStream("/assets/file2.png")));
            historyCell.getChildren().add(icon);

            Label label = new Label("页面与接口交互文档页面与接口交互文档");
            label.setLayoutX(40);
            label.setLayoutY(5);
            label.setPrefHeight(15);
            label.setPrefWidth(230);
            label.setTextAlignment(TextAlignment.LEFT);
            label.setTextFill(Paint.valueOf("#ffffff"));
            label.setStyle("-fx-font-size: 14;-fx-font-family: arial;");
            historyCell.getChildren().add(label);

            Label label2 = new Label("~/Document/lion/Document/lion/Document/lion/apidoc");
            label2.setLayoutX(40);
            label2.setLayoutY(20);
            label2.setPrefHeight(15);
            label2.setPrefWidth(230);
            label2.setTextAlignment(TextAlignment.LEFT);
            label2.setTextFill(Paint.valueOf("#a5a2a5"));
            label2.setStyle("-fx-font-size: 12;-fx-font-family: arial;");
            historyCell.getChildren().add(label2);
            CellEventHandler handler = new CellEventHandler();
            handler.index = i;
            handler.pane = rightPane;
            handler.cell = historyCell;
            handler.stage = stage;
            handler.scene = scene;
            historyCell.setOnMouseClicked(handler);
            rightPane.getChildren().add(historyCell);
        }
    }

    @FXML
    private void closeAction() {
        stage.close();
    }

    @FXML
    private void createAction() throws IOException, DocumentException {
        System.out.println("create action");
        FileChooser fileChooser = new FileChooser();
        String fileName = "apidoc.xml";
        fileChooser.setInitialFileName(fileName);
        File file = fileChooser.showSaveDialog(stage);

        if (file != null) {
            String path = file.getAbsolutePath();
            System.out.println("getAbsolutePath =" + path);
            Document document = DocumentHelper.createDocument();
            Element root = document.addElement("projects");
            root.addElement("soft-author").addText("coooliang");
            root.addElement("version").addText("0.0.1");
            XMLWriter xmlWriter = new XMLWriter(new FileWriter(path), OutputFormat.createPrettyPrint());
            xmlWriter.write(document);
            xmlWriter.close();

            new RouteUtil(stage).skipToMain(file);
        }
    }

    @FXML
    private void openAction() throws DocumentException, IOException {
        System.out.println("open action");
        FileChooser fileChooser = new FileChooser();
        File file = fileChooser.showOpenDialog(stage);
        new RouteUtil(stage).skipToMain(file);
    }

    private class CellEventHandler implements EventHandler<MouseEvent> {
        public Stage stage;
        public Scene scene;
        public Pane pane;
        public Pane cell;

        public int index;

        public Document document;

        @Override
        public void handle(MouseEvent mouseEvent) {
            List<Pane> panes = (List) this.pane.getChildren();
            for (Pane pane : panes) {
                pane.setStyle("-fx-background-color: #2f2b36;");
                Label label2 = (Label) pane.getChildren().get(2);
                label2.setTextFill(Paint.valueOf("#a5a2a5"));
            }
            Label label2 = (Label) this.cell.getChildren().get(2);
            label2.setTextFill(Paint.valueOf("#ffffff"));
            this.cell.setStyle("-fx-background-color: #1c61cb;-fx-background-radius: 5;");
            if (mouseEvent.getClickCount() == 2) {
                openProjectAction(index);
            }
        }

        private void openProjectAction(int index) {
            System.out.println("double click");
            System.out.println("index = " + index);
            try {
                new RouteUtil(stage).skipToMain(null);
            } catch (IOException | DocumentException e) {
                e.printStackTrace();
            }
        }
    }
}

